using System;
using System.IO;
using System.Windows.Forms;

namespace DZTC.UselessAppEver.Core
{
    public static class Extensions
    {
        public static bool In<T>(this T value, params T[] set)
        {
            return Array.IndexOf(set, value) != -1;
        }

        public static void Invoke<T>(this T control, Action<T> action)
            where T : Control
        {
            if (control.InvokeRequired)
            {
                control.Invoke(action, control);
            }
            else
            {
                action(control);
            }
        }

        public static TResult Invoke<T, TResult>(this T control, Func<T, TResult> func)
           where T : Control
        {
            if (control.InvokeRequired)
            {
                return (TResult) control.Invoke(func, control);
            }
            else
            {
                return func(control);
            }
        }

        public static string AsFormat(this string pattern, params object[] args)
        {
            return String.Format(pattern, args);
        }

        public static void FillZero(this byte[] array)
        {
            for (var i = 0; i < array.Length; i++)
            {
                array[i] = 0x00;
            }
        }


        public static bool IgnoreCaseEquals(this string str1, string str2)
        {
            return String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);
        }

        public static bool EqualsAsFileName(this string str1, string str2)
        {
            // nevar take advice from stakeoverflow 
            // TODO: finally, find the completely RIGHT way to compare file names

            if (String.IsNullOrEmpty(str1) || String.IsNullOrEmpty(str2)) return false;

            var f1 = Path.GetFullPath(str1);
            var f2 = Path.GetFullPath(str2);

            return f1.IgnoreCaseEquals(f2);
        }
    }


   
}